package com.isoftstone.bank.controller;


import com.isoftstone.bank.controller.base.BaseController;
import com.isoftstone.bank.entity.BOrg;
import com.isoftstone.bank.entity.RptMerchantStatistics;
import com.isoftstone.bank.service.impl.BOrgService;
import com.isoftstone.common.Page;
import com.isoftstone.common.Tidings;
import com.isoftstone.util.Const;
import com.isoftstone.util.ObjectExcelView;
import com.isoftstone.util.PageData;
import com.isoftstone.util.TreeNode;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.lang.reflect.Method;
import java.util.*;

/**
 * Created by xwj on 2017/11/28.
 */
@Controller
public class OrgController extends BaseController {

        @Autowired
        private BOrgService service;

        @RequiresPermissions("bms-menu.001.001")
        @RequestMapping("/bank/org")
        public ModelAndView toOrgPage(Page page){
                return new ModelAndView("/bank/org/org_list");
        }

        @RequestMapping("/bank/org/getOrgTree")
        @ResponseBody
        public Object getOrgTree(){
                Tidings<List<TreeNode>> tidings = new Tidings<>();
                //List<TreeNode> list = new ArrayList<>();
                tidings.setT(service.selectOrgTreeNodeBySupbak(0L,true));
                tidings.setStatus(Const.SUCCESS);
                return tidings;
        }

        @RequestMapping("/bank/org/getUserSelectOrgTree")
        @ResponseBody
        public Object getUserSelectOrgTree(){
                Tidings<List<TreeNode>> tidings = new Tidings<>();
                tidings.setT(service.selectOrgTreeNodeByOrgId(Long.valueOf(this.getUser().getCorpId())));
                tidings.setStatus(Const.SUCCESS);
                return tidings;
        }

        @RequestMapping("/bank/org/getMoveOrgTree")
        @ResponseBody
        public Object getMoveOrgTree(String orgId){
                Tidings<List<TreeNode>> tidings = new Tidings<>();
                tidings.setT(service.selectMoveOrgTree(0L,Long.valueOf(orgId)));
                tidings.setStatus(Const.SUCCESS);
                return tidings;
        }
        @RequestMapping("/bank/org/save")
        @ResponseBody
        public synchronized Object save(@ModelAttribute("org") BOrg org){
                Tidings<TreeNode> tidings = new Tidings<>();
                try {
                        if(service.orgNameIsExsit(org.getOrgName(),org.getOrgId())){
                                tidings.setStatus(Const.ERROR);
                                tidings.setMsg("该机构名称已被占用，请更换！");
                                return tidings;
                        }
                        if(org.getOrgId() == null){
                                BOrg parent = service.selectByPrimaryKey(org.getSupbak());
                                int orgLevel = Integer.valueOf(parent.getOrgLevel())+1;
                                org.setOrgId(service.selectNextOrgId());
                                org.setOrgLevel(orgLevel+"");
                                org.setLevel1(parent.getLevel1());
                                org.setLevel2(parent.getLevel2());
                                org.setLevel3(parent.getLevel3());
                                org.setLname1(parent.getLname1());
                                org.setLname2(parent.getLname2());
                                org.setLname3(parent.getLname3());
                                Method setLevel = BOrg.class.getMethod("setLevel"+orgLevel,Long.class);
                                Method setLname = BOrg.class.getMethod("setLname"+orgLevel,String.class);
                                setLevel.invoke(org,org.getOrgId());
                                setLname.invoke(org,org.getOrgName());
                                org.setCreator(this.getUser().getUserid());
                                org.setCreateDate(new Date());
                                org.setUpdateIp(getRequest().getRemoteAddr());
                                service.insertSelective(org);
                                tidings.setToken("add");
                        }else{
                                org.setUpdator(this.getUser().getUserid());
                                org.setUpdateDate(new Date());
                                org.setUpdateIp(getRequest().getRemoteAddr());
                                service.updateByPrimaryKeySelective(org);
                                tidings.setToken("edit");
                        }
                        TreeNode node = new TreeNode();
                        node.setId(org.getOrgId()+"");
                        node.setLabel(org.getOrgName());
                        node.setLevel(org.getOrgLevel());
                        node.setChildren(service.selectOrgTreeNodeBySupbak(org.getOrgId(),true));
                        node.setLeaf(node.getChildren() == null);
                        tidings.setT(node);
                        tidings.setStatus(Const.SUCCESS);
                } catch (Exception e) {
                        e.printStackTrace();
                        tidings.setStatus(Const.ERROR);
                        tidings.setMsg("保存失败！");
                }
                return tidings;
        }
        @RequestMapping("/bank/org/saveMove")
        @ResponseBody
        public synchronized Object saveMove(@ModelAttribute("org") BOrg org){
                Tidings<TreeNode> tidings = new Tidings<>();
                try {
                        BOrg parent = service.selectByPrimaryKey(org.getSupbak());
                        int orgLevel = Integer.valueOf(parent.getOrgLevel())+1;
                        org.setOrgLevel(orgLevel+"");
                        org.setLevel1(parent.getLevel1());
                        org.setLevel2(parent.getLevel2());
                        org.setLevel3(parent.getLevel3());
                        org.setLname1(parent.getLname1());
                        org.setLname2(parent.getLname2());
                        org.setLname3(parent.getLname3());
                        Method setLevel = BOrg.class.getMethod("setLevel"+orgLevel,Long.class);
                        Method setLname = BOrg.class.getMethod("setLname"+orgLevel,String.class);
                        setLevel.invoke(org,org.getOrgId());
                        setLname.invoke(org,org.getOrgName());
                        org.setUpdator(this.getUser().getUserid());
                        org.setUpdateDate(new Date());
                        org.setUpdateIp(getRequest().getRemoteAddr());
                        service.updateByPrimaryKeySelective(org);
                        tidings.setStatus(Const.SUCCESS);
                } catch (Exception e) {
                        e.printStackTrace();
                        tidings.setStatus(Const.ERROR);
                        tidings.setMsg("保存失败！");
                }
                return tidings;
        }
        @RequestMapping("/bank/org/del")
        @ResponseBody
        public synchronized Object del(Long orgId){
                Tidings<String> tidings = new Tidings<String>();
                try {
                        service.deleteByPrimaryKey(orgId);
                        tidings.setStatus(Const.SUCCESS);
                        tidings.setMsg("删除成功！");
                } catch (Exception e) {
                        tidings.setStatus(Const.ERROR);
                        tidings.setMsg("删除失败！");
                        e.printStackTrace();
                }
                return tidings;
        }
        @RequestMapping("/bank/org/exportExcel")
        public ModelAndView exportExcel(){
                ModelAndView md = null;
                try {
                        List<BOrg> templist = service.exportExcel();
                        Map<String,Object> dataMap = new HashMap<String,Object>();
                        List<String> titles = new ArrayList<String>();
                        titles.add("机构编号");
                        titles.add("机构名称");
                        titles.add("机构层级");
                        titles.add("上级机构号");
                        dataMap.put("titles", titles);
                        List<PageData> subList = new ArrayList<PageData>();
                        for (BOrg rpt:templist) {
                                PageData tmpPd = new PageData();
                                tmpPd.put("var1",rpt.getOrgId()+"");
                                tmpPd.put("var2",rpt.getOrgName());
                                tmpPd.put("var3",rpt.getOrgLevel());
                                tmpPd.put("var4",rpt.getSupbak()+"");
                                subList.add(tmpPd);
                        }
                        dataMap.put("varList", subList);
                        dataMap.put("name","机构列表导出");
                        dataMap.put("sheetName","机构列表导出");
                        ObjectExcelView erv = new ObjectExcelView();
                        md = new ModelAndView(erv,dataMap);
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return md;
        }

}
